package com.zz.bid.web.controller;

import com.zz.bid.service.BidService;
import com.zz.bid.to.BidTO;
import com.zz.bid.to.ResponseWrapper;
import com.zz.bid.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chenkehan
 * created at 2020/4/4  12:50 下午
 */
@RestController
public class BidController extends BaseController {

    @Autowired
    private BidService bidService;

    @PostMapping("createBid")
    public ResponseWrapper create(@RequestBody BidTO bidTO) {
        try {
            bidService.create(bidTO);
            return ResponseWrapper.builder().status(HttpStatus.OK.value()).path(request.getServletPath()).build();
        } catch (Exception e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().status(HttpStatus.INTERNAL_SERVER_ERROR.value()).path(request.getServletPath()).message(e.getMessage()).build();
        }
    }

    @PostMapping("searchBid")
    public ResponseWrapper search(@RequestBody BidTO bidTO) {
        try {
            List<BidTO> bidTOs = bidService.search(bidTO);
            return ResponseWrapper.builder().path(request.getServletPath()).data(bidTOs).status(HttpStatus.OK.value()).build();
        } catch (Exception e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().status(HttpStatus.INTERNAL_SERVER_ERROR.value()).
                    message(e.getMessage()).path(request.getContextPath()).build();
        }
    }

}
